import { Component,OnChanges ,Input,SimpleChanges} from '@angular/core';

@Component({
  selector: 'app-child2',
  template:`
    <h3>版本 {{major}}.{{minor}}</h3>
    <h4>改变记录：</h4>
    <ul>
      <li *ngFor="let change of changeLog">{{change}}</li>
    </ul>
  `
})
export class Child2Component implements OnChanges{

  @Input() major:any;
  @Input() minor:any;

  changeLog:string[]=[];
  

  constructor() { }


  ngOnChanges(changes:SimpleChanges) {
    const log:string[] = [];
    for (const propName in changes) {
      const changedProp = changes[propName];
      const to = JSON.stringify(changedProp.currentValue);
      if (changedProp.isFirstChange()) {
        log.push(`第一次的值是${propName}设为${to}`);
      }else {
        const from = JSON.stringify(changedProp.previousValue);
        log.push(`${propName} 从 ${from} 成为 ${to}`);
      }
    }
    this.changeLog.push(log.join(','));
  }

}
